package com.fezz.boot.rpc.proxy;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.log4j.Log4j2;

import java.util.Date;

/**
 * 客户端处理类
 *
 * @author muyun
 * @version 1.0.0
 * @since 2020/7/6
 */
@Log4j2
public class ClientHandler extends ChannelInboundHandlerAdapter {
    private Object response;

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("CilentHandler ChannelActive. time: " + new Date());
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        log.info("ClientHandler msg : {}", msg);
        response = msg;
        log.info("ClientHandler server response : {}", this.response);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        log.error("client happens exception : {}", cause.getLocalizedMessage());
    }

    public Object getResp() {
        return this.response;
    }

}
